package com.dudu.dataImport;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import net.sf.json.JSONArray;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

public class SmsPostUtil {
	/**
	 * 根据组织账号导入
	 * 从sourceConn中t_pool_account_rel、t_account中根据条件导入到
	 * targetConn中的t_pool_account_rel、t_rule_account
	 * @param sourceConn 源数据库连接
	 * @param targetConn 目的数据库连接
	 * @param appid  应用ID,多个使用逗号分隔
	 */
	public void dataConversion(Connection sourceConn){
		Statement stmt = null;
		Statement stmt_rel = null;
		Statement stmt_rule=null;
		MyUtil myUtil = new MyUtil();
		try{
			stmt=sourceConn.createStatement();
			/*sql 拼接*/
			StringBuffer sql=new StringBuffer();
			sql.append("select phone from t_phone ");
			/*执行查询*/
			ResultSet accountresult = stmt.executeQuery(sql.toString());
			/*打印总条数*/
			accountresult.last();//移到最后一行   
			int rowCount = accountresult.getRow(); //得到当前行号，也就是记录数   
			accountresult.beforeFirst(); //如果还要用结果集，就把指针再移到初始化的位置
			System.out.println("accountresultsql="+sql.toString()+",resultCount="+rowCount);
			if(rowCount>0){
				/*遍历accountresult*/
				while (accountresult.next()) {
					/*获取数据*/
					String phone=myUtil.isNull(accountresult.getString("phone"));
					this.send(phone);
				}
				stmt.close();
			    sourceConn.close();
			}
		}catch(Exception e){
			try {
				sourceConn.close();
		        stmt.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	
	public void send(String phone){ 
		JSONArray jsonArray = new JSONArray();
		jsonArray.add("51089693");
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod("http://123.56.2.91:8080/SMSv4/postSms.do");
		post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");// 在头文件中设置转码
		NameValuePair[] data = { 
			new NameValuePair("app_key", "99653B0C7EC6897C2F426C0E13A9D66C"),
			new NameValuePair("cust_account", "1464225803014_8866"),
			new NameValuePair("timestamp", "1481505050643"),
			new NameValuePair("sign", "919fd3e1be6709a82a654d5c0c343d5cde019887"),
			new NameValuePair("ext_terminalCode", "18060249922"),
			new NameValuePair("called", phone),
			new NameValuePair("template_code", "template_bs1212"),
			new NameValuePair("msg_id", ""),
			new NameValuePair("template_params", jsonArray.toString())
		};
		post.setRequestBody(data);
	
		try {
			client.executeMethod(post);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Header[] headers = (Header[]) post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println(h.toString());
		}
		String result = null;
		try {
			result = new String(post.getResponseBodyAsString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(result);
		post.releaseConnection();
	}
	
	/**
	 * 每次发送前记得确认短信内容
	 * @param args
	 */
	public static void main(String[] args) {
		/*源*/
		String sourceUrl="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8";
		String sourceUsername="root";
		String sourcePassword="123456";
		/*创建连接*/
		DBConnectionUtil dbUtil = new DBConnectionUtil();
		Connection sourceConn = dbUtil.getDBConnection(sourceUrl,sourceUsername,sourcePassword);
		
		SmsPostUtil util=new SmsPostUtil();
		util.dataConversion(sourceConn);
		//util.send("17001090122");
		System.out.println("执行完成执行以下sql");
	}

}
